<template>
  <a-modal
    title='运行日志'
    :width='800'
    :visible='visible'
    :confirmLoading='loading'
    :footer='null'
    @cancel="() => { $emit('cancel') }"
  >
    <div class='query-container'>

    </div>

    <div class='main-container'>
      <s-table
        ref='log-table'
        size='default'
        rowKey='id'
        :scroll='{  y: 400 }'
        :columns='columns'
        :data='loadData'
      >
        <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      </s-table>
    </div>
  </a-modal>
</template>

<script>
import { findTriggerLogsByPage } from '@/api/quartz'
import { STable } from '@/components'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '执行开始时间',
    dataIndex: 'beginTime'
  },
  {
    title: '执行结束时间',
    dataIndex: 'endTime'
  },
  {
    title: '运行描述',
    dataIndex: 'statusType'
  }
]

export default {
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    },
    loadDataFun: {
      type: Function
    },
    agorigthmTaskId: {
      type: [String, Number],
      default: ''
    }
  },
  watch: {
    agorigthmTaskId(val) {
      this.$nextTick(() => {
        this.$refs['log-table'].refresh()
      })
    }
  },
  data() {
    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 3 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 21 }
      }
    }
    return {
      columns,
      queryParam: {},
      form: this.$form.createForm(this),
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        if (this.agorigthmTaskId) {
          return findTriggerLogsByPage({ ...requestParameters, agorigthmTaskId: this.agorigthmTaskId }).then(res => {
            const tempMap = new Map()
            const list = res.list
            list.forEach(item => {
              tempMap.set(item.id, item)
            })
            return {
              ...res,
              list: [...tempMap.values()]
            }
          })
        }
      }
    }
  },
  components: {
    STable
  },
  created() {
    // 防止表单未注册
  },
  methods: {}
}
</script>
